/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mysql;

import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MeoMupU
 */
public class DataProvider extends GLOBAL.global{
    
    static MySQLConnect conn = new MySQLConnect();
    /**
     * 
     * @param curentpage
     * @return ResultSet
     */
    public static ResultSet loadFeatureProduct(int curentpage){
        try {
            String sql="SELECT * FROM  `sanpham` sp, hinhanh ha WHERE sp.MaSP = ha.MaSP ORDER BY sp.LuotXem  DESC LIMIT "+ (curentpage-1)*HomeProductView +", " +HomeProductView ;
            return conn.excuteQuery(sql);
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 
     * @param curentpage
     * @return ResultSet
     */
    public static ResultSet loadNewProduct(int curentpage){
        try {
            String sql="SELECT * FROM  `sanpham` sp, hinhanh ha WHERE sp.MaSP = ha.MaSP ORDER BY sp.NgayDang  DESC LIMIT "+ (curentpage-1)*HomeProductView+", "+HomeProductView;
            return conn.excuteQuery(sql);
        } catch (Exception e) {
            return null;
        }
    }
    
    /**
     * 
     * @param curentpage
     * @return ResultSet
     */
    public static ResultSet loadTodayProduct(int curentpage){
        try {
            String sql="SELECT * FROM  `sanpham` sp, hinhanh ha WHERE sp.MaSP = ha.MaSP AND DATE_FORMAT(sp.NgayDang,'USA')= DATE_FORMAT(NOW(),'USA')  ORDER BY sp.NgayDang  DESC LIMIT "+ (curentpage-1)*PageProductView +", " +PageProductView ;
            return conn.excuteQuery(sql);
        } catch (Exception e) {
            return null;
        }
    }
    
    /**
     * 
     * @param Cat id loai sản phẩm
     * @return int số phần trăm giảm giá
     */
    public static int getTotalPage(String Cat){
        int curentpage=0;
        if(Cat==null){
            String sql="SELECT * FROM  `sanpham` sp, hinhanh ha WHERE sp.MaSP = ha.MaSP ORDER BY sp.LuotXem  DESC";
            try {
                curentpage = MySQLConnect.coutRow(conn.excuteQuery(sql))/HomeProductView;
                if(MySQLConnect.coutRow(conn.excuteQuery(sql))%HomeProductView !=0)
                    curentpage++;
                return curentpage;
            } catch (Exception ex) {
                Logger.getLogger(DataProvider.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else{
            String sql="SELECT * FROM  `sanpham` sp, hinhanh ha WHERE sp.MaSP = ha.MaSP AND sp.MaLoaiSP= "+Cat+" ORDER BY sp.LuotXem  DESC";
            try {
                curentpage = MySQLConnect.coutRow(conn.excuteQuery(sql))/PageProductView;
                if(MySQLConnect.coutRow(conn.excuteQuery(sql))%PageProductView !=0)
                    curentpage++;
                return curentpage;
            } catch (Exception ex) {
                Logger.getLogger(DataProvider.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return curentpage;
    }
    
    
    /**
     * 
     * @return int số phần trăm giảm giá
     */
    public static int getTotalPageToday(){
        int curentpage=0;
        String sql="SELECT * FROM  `sanpham` sp, hinhanh ha WHERE sp.MaSP = ha.MaSP AND DATE_FORMAT(sp.NgayDang,'USA')= DATE_FORMAT(NOW(),'USA') ORDER BY sp.LuotXem  DESC";
        try {
            curentpage = MySQLConnect.coutRow(conn.excuteQuery(sql))/PageProductView;
            if(MySQLConnect.coutRow(conn.excuteQuery(sql))%PageProductView !=0)
                curentpage++;
            return curentpage;
        } catch (Exception ex) {
            Logger.getLogger(DataProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        return curentpage;
    }
    
    
    /**
     * 
     * @return int số phần trăm giảm giá
     */
    public static int getTotalPageNew(){
        int curentpage=0;
        String sql="SELECT * FROM  `sanpham` sp, hinhanh ha WHERE sp.MaSP = ha.MaSP ORDER BY sp.NgayDang  DESC";
        try {
            curentpage = MySQLConnect.coutRow(conn.excuteQuery(sql))/HomeProductView;
            if(MySQLConnect.coutRow(conn.excuteQuery(sql))%HomeProductView !=0)
                curentpage++;
            return curentpage;
        } catch (Exception ex) {
            Logger.getLogger(DataProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        return curentpage;
    }
    /**
     * 
     * @param cat category get
     * @return ResultSet
     */
    public static ResultSet loadCategory(String cat){
        try {
            String sql="SELECT * FROM loaisanpham WHERE MaLoaiSP = "+cat+" LIMIT 0,1" ;
            if(cat==null){
                sql="SELECT * FROM loaisanpham Order By MaLoaiSP";
            }
            return conn.excuteQuery(sql);
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 
     * @param cat category get
     * @param curentpage
     * @return ResultSet
     */
    public static ResultSet loadProductByCategory(String cat ,int curentpage){
        try {
            String sql="SELECT * FROM  `sanpham` sp, hinhanh ha WHERE sp.MaSP = ha.MaSP AND sp.MaLoaiSP = "+cat+" ORDER BY sp.LuotXem  DESC LIMIT "+ (curentpage-1)*PageProductView +", " +PageProductView ;
            return conn.excuteQuery(sql);
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 
     * @param giagoc giá sản phẩm
     * @param giaban giá bán ra
     * @return int số phần trăm giảm giá
     */
    public static int getDiscout(String giagoc, String giaban){
        float a= Float.parseFloat(giagoc);
        float b= Float.parseFloat(giaban);
        return (int) (((a-b)/a) * 100);
    }
}
